<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="流程标识" prop="piid">
        <el-input
          v-model="queryParams.piid"
          placeholder="请输入流程标识"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务id" prop="tiid">
        <el-input
          v-model="queryParams.tiid"
          placeholder="请输入任务id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务标记" prop="taskTag">
        <el-input
          v-model="queryParams.taskTag"
          placeholder="请输入任务标记"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务状态" prop="state">
        <el-input
          v-model="queryParams.state"
          placeholder="请输入任务状态"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="节点名称" prop="nodeName">
        <el-input
          v-model="queryParams.nodeName"
          placeholder="请输入当前节点名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理人id" prop="handlerId">
        <el-input
          v-model="queryParams.handlerId"
          placeholder="请输入任务处理人uid"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="handlerName">
        <el-input
          v-model="queryParams.handlerName"
          placeholder="请输入任务处理人姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门" prop="handlerDept">
        <el-input
          v-model="queryParams.handlerDept"
          placeholder="请输入部门"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['biz:flowApproveInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['biz:flowApproveInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['biz:flowApproveInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['biz:flowApproveInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="flowApproveInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="流程标识" align="center" prop="piid" />
      <el-table-column label="任务id" align="center" prop="tiid" />
      <el-table-column label="任务标记" align="center" prop="taskTag" />
      <el-table-column label="任务状态" align="center" prop="state" />
      <el-table-column label="当前节点名称" align="center" prop="nodeName" />
      <el-table-column label="任务处理人uid" align="center" prop="handlerId" />
      <el-table-column label="任务处理人姓名" align="center" prop="handlerName" />
      <el-table-column label="部门" align="center" prop="handlerDept" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['biz:flowApproveInfo:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['biz:flowApproveInfo:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改审批记录对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="flowApproveInfoRef" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="流程标识" prop="piid">
          <el-input v-model="form.piid" placeholder="请输入流程标识" />
        </el-form-item>
        <el-form-item label="任务id" prop="tiid">
          <el-input v-model="form.tiid" placeholder="请输入任务id" />
        </el-form-item>
        <el-form-item label="任务标记" prop="taskTag">
          <el-input v-model="form.taskTag" placeholder="请输入任务标记" />
        </el-form-item>
        <el-form-item label="任务状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入任务状态" />
        </el-form-item>
        <el-form-item label="当前节点编码" prop="nodeId">
          <el-input v-model="form.nodeId" placeholder="请输入当前节点编码" />
        </el-form-item>
        <el-form-item label="当前节点名称" prop="nodeName">
          <el-input v-model="form.nodeName" placeholder="请输入当前节点名称" />
        </el-form-item>
        <el-form-item label="任务处理人uid" prop="handlerId">
          <el-input v-model="form.handlerId" placeholder="请输入任务处理人uid" />
        </el-form-item>
        <el-form-item label="任务处理人姓名" prop="handlerName">
          <el-input v-model="form.handlerName" placeholder="请输入任务处理人姓名" />
        </el-form-item>
        <el-form-item label="部门" prop="handlerDept">
          <el-input v-model="form.handlerDept" placeholder="请输入部门" />
        </el-form-item>
        <el-form-item label="处理时间" prop="approveTime">
          <el-date-picker clearable
                          v-model="form.approveTime"
                          type="datetime"
                          value-format="YYYY-MM-DD HH:mm:ss"
                          placeholder="请选择处理时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="决策名称" prop="decisionName">
          <el-input v-model="form.decisionName" placeholder="请输入决策名称" />
        </el-form-item>
        <el-form-item label="处理决策" prop="decision">
          <el-input v-model="form.decision" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="处理意见" prop="notion">
          <el-input v-model="form.notion" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="下一任务id" prop="nextTiid">
          <el-input v-model="form.nextTiid" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="下一节点id" prop="nextNodeId">
          <el-input v-model="form.nextNodeId" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="下一处理节点名" prop="nextNodeName">
          <el-input v-model="form.nextNodeName" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="下一处理人uid" prop="nextHandlerId">
          <el-input v-model="form.nextHandlerId" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="下一处理人姓名" prop="nextHandlerName">
          <el-input v-model="form.nextHandlerName" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="到达时间" prop="arriveTime">
          <el-date-picker clearable
                          v-model="form.arriveTime"
                          type="datetime"
                          value-format="YYYY-MM-DD HH:mm:ss"
                          placeholder="请选择到达时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="处理用时" prop="processingTime">
          <el-input v-model="form.processingTime" placeholder="请输入处理用时" />
        </el-form-item>
        <el-form-item label="累计用时" prop="cumulativeTime">
          <el-input v-model="form.cumulativeTime" placeholder="请输入累计用时" />
        </el-form-item>
        <el-form-item label="是否删除" prop="deleted">
          <el-input v-model="form.deleted" placeholder="请输入是否删除" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { listFlowApproveInfo, getFlowApproveInfo, delFlowApproveInfo, addFlowApproveInfo, updateFlowApproveInfo } from "@/api/biz/flowApproveInfo";

const { proxy } = getCurrentInstance();

const flowApproveInfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    piid: null,
    tiid: null,
    taskTag: null,
    state: null,
    nodeName: null,
    handlerId: null,
    handlerName: null,
    handlerDept: null,
  },
  rules: {
    piid: [{ required: true, message: "流程标识不能为空", trigger: "blur" }],
    tiid: [{ required: true, message: "任务id不能为空", trigger: "blur" }],
    state: [{ required: true, message: "任务状态不能为空", trigger: "blur" }],
    nodeId: [{ required: true, message: "当前节点编码不能为空", trigger: "blur" }],
    handlerId: [{ required: true, message: "任务处理人uid不能为空", trigger: "blur" }],
    handlerName: [{ required: true, message: "任务处理人姓名不能为空", trigger: "blur" }],
    createTime: [{ required: true, message: "创建时间不能为空", trigger: "blur" }],
    updateTime: [{ required: true, message: "修改时间不能为空", trigger: "blur" }],
    arriveTime: [{ required: true, message: "到达时间不能为空", trigger: "blur" }],
    deleted: [{ required: true, message: "是否删除不能为空", trigger: "blur" }],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询审批记录列表 */
async function getList() {
  loading.value = true;
  const response  = await listFlowApproveInfo(queryParams.value);
  flowApproveInfoList.value = response.rows;
  total.value = response.total;
  loading.value = false;
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    piid: null,
    tiid: null,
    taskTag: null,
    state: null,
    nodeId: null,
    nodeName: null,
    handlerId: null,
    handlerName: null,
    handlerDept: null,
    approveTime: null,
    decisionName: null,
    decision: null,
    notion: null,
    nextTiid: null,
    nextNodeId: null,
    nextNodeName: null,
    nextHandlerId: null,
    nextHandlerName: null,
    createTime: null,
    updateTime: null,
    arriveTime: null,
    processingTime: null,
    cumulativeTime: null,
    deleted: null
  };
  proxy.resetForm("flowApproveInfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加审批记录";
}

/** 修改按钮操作 */
async function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  const response = await getFlowApproveInfo(_id);
  form.value = response.data;
  open.value = true;
  title.value = "修改审批记录";
}

/** 提交按钮 */
async function submitForm() {
  try {
    const valid = await proxy.$refs["flowApproveInfoRef"].validate();
    if (valid) {
      if (form.value.id != null) {
        await updateFlowApproveInfo(form.value);
        proxy.$modal.msgSuccess("修改成功");
      } else {
        await addFlowApproveInfo(form.value);
        proxy.$modal.msgSuccess("新增成功");
      }
      open.value = false;
      getList();
    }
  } catch (error) {
    // 校验失败界面自动有红色标记，可以不需要提示； 接口调用失败有接口本身的错误提示
  }
}

/** 删除按钮操作 */
async function handleDelete(row) {
  try {
    const _ids = row.id || ids.value;
    await proxy.$modal.confirm('是否确认删除审批记录编号为"' + _ids + '"的数据项？');
    await delFlowApproveInfo(_ids);
    getList();
    proxy.$modal.msgSuccess("删除成功");
  } catch (error) {
    // 用户取消会走到这里，不需要提示； 接口调用失败有接口本身的错误提示
  }
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('/biz/flowApproveInfo/export', {
    ...queryParams.value
  }, `flowApproveInfo_${new Date().getTime()}.xlsx`)
}

getList();
</script>
